package com.ruoyi.am.controller;

import com.ruoyi.am.service.IAmCarPlaceService;
import com.ruoyi.am.service.IAmDriverService;
import com.ruoyi.am.service.IAmRouteService;
import com.ruoyi.am.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/listen")
public class AmListenController {

    @Autowired
    private IAmDriverService iAmDriverService;
    @Autowired
    private AmRosterController AmRoster;
    @Autowired
    private IAmRouteService iAmRouteService;
    @Autowired
    private IAmCarPlaceService amCarPlaceService;

    private volatile AmServer amServer;

    @GetMapping("/doing")
    public String listening(){
        BeanUtil beanUtil = new BeanUtil(iAmDriverService, AmRoster, iAmRouteService, amCarPlaceService);
        try {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    amServer = new AmServer();
                    amServer.server();
                }
            }).start();
            return "LISTEN SUCCESS!";
        }
        catch (Exception e){
            e.printStackTrace();
            System.out.println("LISTEN ERROR!");
            return "LISTEN ERROR！" + e.getMessage();
        }
    }

    @GetMapping("/stop")
    public String stop(){
        BeanUtil beanUtil = new BeanUtil(iAmDriverService, AmRoster, iAmRouteService, amCarPlaceService);
        try {
            amServer.stop();
            return "STOP SUCCESS!";
        }
        catch (Exception e){
            e.printStackTrace();
            System.out.println("STOP ERROR!");
            return "STOP ERROR！" + e.getMessage();
        }
    }


}
